﻿using Gfw.Models.Account;
using Gfw.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate.Criterion;

namespace Gfw.Repositories.Manage
{
    public class ActCompanyStatisticsprofitRepository : Repository<ActCompanyStatisticsprofit>
    {
        /// <summary>
        /// 增加统计记录
        /// </summary>
        /// <param name="settlementDate">统计日期</param>
        /// <param name="annualFee"></param>
        /// <param name="sellStock"></param>
        /// <param name="toCashTax"></param>
        /// <param name="tax"></param>
        /// <param name="changeInformation"></param>
        /// <param name="bonus"></param>
        /// <param name="others"></param>
        /// <param name="remark"></param>
        /// <param name="unitOfWork"></param>
        public virtual void AddProfit(DateTime settlementDate, decimal annualFee, decimal sellStock, 
            decimal toCashTax, decimal tax, decimal changeInformation,
            decimal bonus, decimal others, 
            string remark = null, UnitOfWork unitOfWork = null)
        {
            ActCompanyStatisticsprofit profit = new ActCompanyStatisticsprofit()
            {
                Id = Guid.NewGuid().ToString(),
                SettlementDate = settlementDate,
                AnnualFee = annualFee,
                SellStock = sellStock,
                ToCashTax = toCashTax,
                Tax = tax,
                ChangeInformation = changeInformation,
                Bonus = bonus,
                Others = others,
                Abstract = remark,

                Total = annualFee + sellStock + toCashTax + tax + changeInformation, // 合计
                Balance = annualFee + sellStock + toCashTax + tax + changeInformation - bonus - others, // 累计结余
            };

            Save(profit, unitOfWork);
        }


        /// <summary>
        /// 查询公司总利润
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalPage"></param>
        /// <returns></returns>
        public virtual IList<ActCompanyStatisticsprofit> Query(DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalPage)
        {
            var query = Session.CreateCriteria("ActCompanyStatisticsprofit");

            if (start.HasValue)
                query = query.Add(Expression.Ge("SettlementDate", start.Value));
            if (end.HasValue)
            {
                end = end.Value.AddDays(1);
                query = query.Add(Expression.Le("SettlementDate", end.Value));
            }

            query = query.AddOrder(new Order("SettlementDate", false));

            totalPage = (query.List().Count + pageSize - 1) / pageSize;
            return query.SetFirstResult(pageIndex * pageSize).SetMaxResults(pageSize).List<ActCompanyStatisticsprofit>();
        }

        
        /// <summary>
        /// 查询该时间范围内的记录
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public virtual IList<ActCompanyStatisticsprofit> Query(DateTime start, DateTime end)
        {
            var query = Session.CreateCriteria("ActCompanyStatisticsprofit")
                .Add(Expression.Ge("SettlementDate", start)).Add(Expression.Le("SettlementDate", end));

            return query.List<ActCompanyStatisticsprofit>();
        }
    }
}
